package com.sduept.nwld.dataserver.service.equipment.assessment;

import com.sduept.bigdata.document.entity.core.CommonDocument;
import com.sduept.bigdata.equipment.assessment.enums.ItemTypeEnum;
import com.sduept.bigdata.equipment.assessment.model.standardlib.StandardInspection;
import com.sduept.bigdata.equipment.assessment.model.template.AssessmentTemplate;
import com.sduept.bigdata.equipment.assessment.model.template.AssessmentTemplateItem;
import com.sduept.bigdata.equipment.assessment.service.AssessmentTemplateService;
import com.sduept.bigdata.equipment.assessment.service.StandardInspectService;
import com.sduept.nwld.dataserver.controller.equipment.assessment.TableViewModelItem;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * 由标准项目、模板生成页面展示所封装的DTO的方法
 * @author huangyu
 * @Date 2019年8月7日
 */
@Slf4j
@Service
public class TableViewModelItemViewService {

	@Autowired
	private StandardInspectService standardService;
	@Autowired
	private AssessmentTemplateService templateService;
	
	/**
	 * 根据标准项目构造页面展示列表
	 * @param stand 标准项目的类型必须为ItemTypeEnum.ROOTNAME
	 * @return 
	 */
	public List<TableViewModelItem> getTableViewModelItemListByStandard(StandardInspection stand) {
		if(null == stand || stand.getItemType() != ItemTypeEnum.ROOTNAME) {
			return null;
		}
		List<TableViewModelItem> selectLists = new ArrayList<TableViewModelItem>();
		// INSPECTIONPROJECT，一、基础管理，二、保护及二次回路
		List<StandardInspection> standards2 = standardService.findByParentId(stand.getId());
		for (StandardInspection s2 : standards2) {
			// 1.1 技术资料管理
			List<StandardInspection> standards3 = standardService.findByParentId(s2.getId());
			if(standards3.size()==0) {
				TableViewModelItem tmvo = new TableViewModelItem(s2);
				selectLists.add(tmvo);
				continue;
			}
			for (StandardInspection s3 : standards3) {
				Set<CommonDocument> documents = new HashSet<CommonDocument>();
				documents.addAll(s3.getDocument());
				// 1.1.1  竣工图的完整性
				List<StandardInspection> standards4 = standardService.findByParentId(s3.getId());
				if(standards4.size()==0) {
					TableViewModelItem tmvo = new TableViewModelItem(s3);
					tmvo.setDocuments(documents);
					selectLists.add(tmvo);
					continue;
				}
				for (StandardInspection s4 : standards4) {
					documents.addAll(s4.getDocument());
					// 具体扣分信息
					List<StandardInspection> standards5 = standardService.findByParentId(s4.getId());
					if(standards5.size()==0) {
						TableViewModelItem tmvo = new TableViewModelItem(s4);
						tmvo.setDocuments(documents);
						selectLists.add(tmvo);
						continue;
					}
					for (StandardInspection s5 : standards5) {
						documents.addAll(s5.getDocument());
						TableViewModelItem tmvo = new TableViewModelItem(s5);
						tmvo.setDocuments(documents);
						selectLists.add(tmvo);
					}
				}
			}
		}
		TableViewModelItem.sortTableViewModelItem(selectLists);
		return selectLists;
	}
		
	/**
	 * 根据模板项构造展示表
	 * @param templateItem
	 * @return
	 */
	public List<TableViewModelItem> getTableViewModelItemListBytemplate(AssessmentTemplateItem templateItem) {
		if(null == templateItem) {
			return null;
		}
		List<TableViewModelItem> templateDetails = new ArrayList<TableViewModelItem>();
		// 所有需要展示的标准检查项目
		Set<StandardInspection> stands = getStandardsByTemplateItem(templateItem);
		try {
			StandardInspection root = getStandardVersionByTemplateItem(templateItem);
			recursionGetChild(root, stands,templateDetails);
			TableViewModelItem.sortTableViewModelItem(templateDetails);
			return templateDetails;
		} catch (Exception e) {
			log.info("获取可用版本数据过程中发生错误");
			e.printStackTrace();
			return null;
		}
	}
	
	/**
	 * 通过模板项获取对应的评价项目的版本
	 * @param templateItem
	 * @return
	 */
	private StandardInspection getStandardVersionByTemplateItem(AssessmentTemplateItem templateItem) {
		try {
			// 通过模板项获取模板
			AssessmentTemplate template = templateService.findAssessmentTemplatesByItem(templateItem.getId());
			if(null == template) {
				return null;
			}
			// 获取标准项目的版本
			String standardVersion = template.getStandardVersion();
			StandardInspection stand = standardService.findById(standardVersion);
			return stand;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	/**
	 * 获取模板的所有检查项目（一并获取检查项目的父、子）
	 * @param item
	 * @return
	 */
	private Set<StandardInspection> getStandardsByTemplateItem(AssessmentTemplateItem item) {
		// 获取该模板的检查项目，并获取每个检查项目的父与子，
		try {
			List<StandardInspection> stands = templateService.findStandardInspectionbyItem(item.getId());
			Set<StandardInspection> allStands = new HashSet<StandardInspection>();
			if(null != stands) {
				allStands.addAll(stands);
				for (StandardInspection stand : stands) {
					recursionGetParent(stand, allStands);
					recursionGetChildren(stand, allStands);
				}
			}
			return allStands;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	/**
	 * 递归获取父
	 * @param stand
	 * @param allStands
	 */
	private void recursionGetParent(StandardInspection stand,Set<StandardInspection> allStands) {
		StandardInspection pStand = standardService.findByChild(stand.getId());
		if(pStand != null) {
			allStands.add(pStand);
			recursionGetParent(pStand,allStands);
		}
	}
	/**
	 * 递归获取子
	 * @param stand
	 * @param allStands
	 */
	private void recursionGetChildren(StandardInspection stand,Set<StandardInspection> allStands) {
		List<StandardInspection> childStands = standardService.findByParentId(stand.getId());
		if(childStands != null) {
			allStands.addAll(childStands);
			for (StandardInspection child : childStands) {
				recursionGetChildren(child, allStands);
			}
		}
	}
	
	/**
	 * 从指定的Set<StandardInspection>中递归获取子
	 * @param stand
	 * @param all
	 */
	private void recursionGetChild(StandardInspection stand,Set<StandardInspection> all,List<TableViewModelItem> templateDetails) {
		if(null != stand) {
			List<StandardInspection> childs = standardService.findByParentId(stand.getId());
			if(stand.getItemType() == ItemTypeEnum.INSPECTIONCRITERION) {
				if(all.contains(stand)) {
					TableViewModelItem tmvo = new TableViewModelItem(stand);
					templateDetails.add(tmvo);
				}
			} else {
				for (StandardInspection s : childs) {
					if(all.contains(s)) {
						recursionGetChild(s,all,templateDetails);
					}
				}
			}
		}
	}
}
